package com.itms.tool;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.itms.tool.TopicChooserDialogFragment.TopicInfo;
import com.itms.tool.TopicChooserDialogFragment.TopicsChooseListener;
import com.itms.tool.data.Category;
import com.itms.tool.data.Object;

public class PrimaryFragment extends ObjectFragment implements TopicsChooseListener {
	
	private EditText mName;
	private RatingBar mRating; 
	private Spinner mCategories;
	private Spinner mSubCategories;
	private CategoryAdapter mCategoryAdapter;
	private CategoryAdapter mSubCategoryAdapter;
	private Category mInitialObjectCategory;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_primary, null);
		setupPrimaryView(v);
		return v;
	}

	private void setupPrimaryView(View v) {
		Button btn = (Button) v.findViewById(R.id.topics);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TopicChooserDialogFragment tcdf = TopicChooserDialogFragment.getInstance();
				tcdf.setTargetFragment(PrimaryFragment.this, 10);
				tcdf.show(getFragmentManager(), "topic_chooser");
			}
		});
		mName = (EditText) v.findViewById(R.id.name);
		mRating = (RatingBar) v.findViewById(R.id.rating);
		mCategories = (Spinner) v.findViewById(R.id.categories);
		mSubCategories = (Spinner) v.findViewById(R.id.sub_categories);
		mCategories.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				Category parentCategory = mCategoryAdapter.getItem(position);
				if (parentCategory != null) {
					List<Category> subCategories = getSubCategories(parentCategory);
					mSubCategoryAdapter.clear();
					mSubCategoryAdapter.addAll(subCategories);
				} // else it is header
				if (mInitialObjectCategory != null) {
					mSubCategories.setSelection(mSubCategoryAdapter.getPosition(mInitialObjectCategory));
					mInitialObjectCategory = null;
				} else {
					//mSubCategories.setSelection(0);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
		mCategoryAdapter = new CategoryAdapter(getActivity());
		mCategoryAdapter.setHeader(R.string.choose_category);
		mSubCategoryAdapter = new CategoryAdapter(getActivity());
		mSubCategoryAdapter.setHeader(R.string.choose_sub_category);
		mCategories.setAdapter(mCategoryAdapter);
		mSubCategories.setAdapter(mSubCategoryAdapter);
	}
	
	@Override
	protected void initContentView(Object object, Bundle savedInstanceState) {
		mName.setText(object.getName());
		mRating.setRating(object.getRating());
		mCategoryAdapter.addAll(getCategories());
		mInitialObjectCategory = object.getCategory();
		if (mInitialObjectCategory != null) {
			mCategories.setSelection(mCategoryAdapter.getPosition(mInitialObjectCategory.getParentCategory()));
		}
	}
	
	@Override
	protected void saveObject(Object object) {
		object.setName(mName.getText().toString());
		object.setRating(mRating.getRating());
		object.setCategory((Category) mSubCategories.getSelectedItem());
	}

	private List<Category> getCategories() {
		try {
			return getHelper().getCategoryDao().queryBuilder().where().isNull(Category.PARENT).query();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	private List<Category> getSubCategories(Category parent) {
		return getHelper().getCategoryDao().queryForEq(Category.PARENT, parent);
	}
	
	@Override
	public void onTopicsChosen(List<TopicInfo> topics) {
		Toast.makeText(getActivity(), "Hola", Toast.LENGTH_SHORT).show();
	}
	
	static class CategoryAdapter extends BaseAdapter {

		private Context mContext;
		private List<Category> mCategories = new ArrayList<Category>();
		private int mDropDownRes = android.R.layout.simple_list_item_1;
		private boolean mNotifyOnChange = true;
		private String mHeaderText;
		
		public CategoryAdapter(Context context) {
			mContext = context;
		}
		
		public void setHeader(String header) {
			mHeaderText = header;
		}
		
		public void setHeader(int resId) {
			mHeaderText = mContext.getString(resId);
		}
		
		public int getPosition(Category category) {
			return mCategories.indexOf(category) + 1;
		}
		
		@Override
		public void notifyDataSetChanged() {
			if (mNotifyOnChange) {
				super.notifyDataSetChanged();
			}
		}
		
		public void add(Category category) {
			mCategories.add(category);
			notifyDataSetChanged();
		}
		
		public void addAll(Collection<? extends Category> categories) {
			mCategories.addAll(categories);
			notifyDataSetChanged();
		}
		
		public void clear() {
			mCategories.clear();
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			return mCategories.size() + 1;
		}

		@Override
		public Category getItem(int position) {
			if (position == 0) {
				return null;
			}
			return mCategories.get(position - 1);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(mDropDownRes, null);
			}
			TextView tv = (TextView) convertView;
			if (position == 0) {
				tv.setText(mHeaderText);
			} else {
				tv.setText(getItem(position).getName());
			}
			return tv;
		}
		
	}
}
